package com.wq.base.controller;

import com.wq.base.entity.Label;
import com.wq.base.service.LabelService;
import com.wq.common.entity.PageResult;
import com.wq.common.entity.Result;
import com.wq.common.entity.StatusCode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("label")
public class LabelController{
    @Autowired
    private LabelService labelService;

    @GetMapping("{id}")
    public Result queryById(@PathVariable String id) {
        Label label = labelService.queryById(id);
        return new Result(true, StatusCode.OK, "查询成功", label);
    }

    @GetMapping
    public Result findAll() {
        List<Label> all = labelService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", all);

    }

    @PostMapping("search/{page}/{size}")
    public Result search(@RequestBody Map map, @PathVariable int page, @PathVariable int size) {
        Page page1 = labelService.search(map, page, size);
        return new Result(true, StatusCode.OK, "分页查询成功", new PageResult<Label>(page1.getTotal(), page1.getRecords()));
    }

    @PostMapping
    public Result save(@RequestBody Label label) {
        labelService.save(label);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    @PutMapping("{id}")
    public Result update(@RequestBody Label label, @PathVariable String id) {
        label.setId(id);
        labelService.update(label);
        return new Result(true, StatusCode.OK, "更新成功");
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable String id) {
        labelService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}